package com.syni.mdd.yhd.common.all.controller.protect;

import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckParams;
import com.syni.mdd.sjb.common.component.utils.common.annotations.EntranceCheck;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.dto.requestDto.BaseReDto;
import com.syni.mdd.yhd.common.all.entity.BmsBusiness;
import com.syni.mdd.yhd.common.all.entity.user.BmsUser;
import com.syni.mdd.yhd.common.all.service.BmsBusinessService;
import com.syni.mdd.yhd.common.all.service.BmsDxOrderService;
import com.syni.mdd.yhd.common.all.service.BmsDxOrderStatusService;
import com.syni.mdd.yhd.common.all.service.BmsUserVideoDkService;
import com.syni.mdd.yhd.common.all.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @className BmsUserVideoLikeController
 * @description 用户与商家打卡
 * Author cfx
 * @DATE 2019/4/8 9:42
 * @VERSION 1.0
 **/

@RestController
@RequestMapping("/userVideoDk")
public class BmsUserVideoDkController {

    @Autowired
    BmsUserVideoDkService bmsUserVideoDkService;

    @Autowired
    BmsBusinessService bmsBusinessService;

    @Autowired
    BmsDxOrderStatusService bmsDxOrderStatusService;

    @Autowired
    BmsDxOrderService bmsDxOrderService;

    /**
     * @Description: 打卡/取消打卡
     * @Param: open_id（用户id）
     * @Param: video_id（视频id）
     * @return: Map
     * @Author: cfx
     * @Date: 2019/4/9
     */
    @CheckParams(checkAuth = true,params = {"video_id"})
    //@ControllerLog(operationAction = "打卡/取消打卡")
    @PostMapping("/dkGivingDk")
    public Map<String, Object> dkOrGivingDk(@RequestBody Map<String,Object> paramsMap){
        Integer videoId = Integer.parseInt(paramsMap.get("video_id").toString());
        Integer userid = Integer.parseInt(paramsMap.get("userIdentify").toString());
        if(userid == 0 || videoId == 0){
            return ResultMapHelper.failure("用户id或视频id不能为空或者零！");
        }
        return bmsUserVideoDkService.operationDk(userid,videoId);
    }

    /**
     * @Description:获取用户打卡清单
     * @Param:user_id
     * @param:page
     * @Author: cfx
     * @Date: 2019/4/8
     */
    @CheckParams(checkAuth = true)
    //@ControllerLog(operationAction = "获取用户打卡清单")
    @PostMapping("/getDkList")
    public Map<String, Object> dkList(@RequestBody ParamsMap paramsMap){
        Integer userId = paramsMap.getInteger("userIdentify");
        String addrCity = "";
        if(StringUtils.checkNullAndEmpty(paramsMap.get("addr_city"))){
            addrCity = paramsMap.getString("addr_city");
        }
        List<Map<String,Object>> bmsVideos = bmsUserVideoDkService.getListToFront(userId,addrCity);
        Map<String,Object> userData = new HashMap<>();
        //统计附加数据
        userData.put("dkNum",bmsUserVideoDkService.findUserTotalDk(userId));
        //城市
        userData.put("cityNum",Constants.COMMON_FLAG_FALSE_INT);
        //查询打卡过的店铺id
        List<Map<String,Object>> resultList = bmsUserVideoDkService.getBaseDao().findByUserId(userId);
        if(resultList != null && resultList.size() > 0){
            Set<Integer> businessIds = new HashSet<>();
            for(Map<String,Object> res : resultList){
                businessIds.add(Integer.parseInt(res.get("bms_business_id").toString()));
            }
            if(businessIds.size() > 0){
                Map<String,Object> conditionMap = new HashMap<>();
                conditionMap.put("id_IN",businessIds);
                List<BmsBusiness> bmsBusinesses = bmsBusinessService.getBaseDao().findAll(new SpecificationUtils(conditionMap));
                Map<String,Integer> cityMap = new HashMap<>();
                if(bmsBusinesses.size() > 0){
                    for(BmsBusiness business : bmsBusinesses){
                        cityMap.put(business.getAddrCity(),Constants.COMMON_FLAG_FALSE_INT);
                    }
                    userData.put("cityNum",cityMap.size());
                }

            }
        }
        //总消费
        userData.put("total",bmsDxOrderService.countUserTotalConsum(userId));
        return ResultMapHelper.success(bmsVideos,userData,"获取成功");
    }

    @GetMapping("/getUserDkCountData")
    public Boolean getUserDkCountData(@RequestParam(required = false) String open_id,
                                      @RequestParam(required = false) Integer user_id){
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        BmsUser bmsUser = null;
        if(StringUtils.isNotBlank(open_id)){
            bmsUser = TokenManager.getUser(open_id);
        }else if(StringUtils.isNotBlank(user_id.toString())){
            bmsUser = TokenManager.getUser(user_id);
        }
        if(bmsUser == null){
            return false;
        }
        bmsUserVideoDkService.countUserStaticDk(bmsUser,response);
        return true;
    }


    @PostMapping("/getUserAgentShop")
    @ResponseBody
    @EntranceCheck(checkAuth = true)
    public Map<String,Object> getUserAgentShop(@RequestBody BaseReDto baseReDto){
        Integer userId = Integer.parseInt(baseReDto.getUserId());
        return bmsUserVideoDkService.getUserAgentShop(userId);
    }

    @PostMapping("/checkUserIsDk")
    @ResponseBody
    @CheckParams(checkAuth = true,params = {"businessId"})
    public Map<String,Object> checkUserIsDk(@RequestBody ParamsMap paramsMap){
        Integer userId = paramsMap.getInteger("userIdentify");
        Integer businessId = paramsMap.getInteger("businessId");
        return bmsUserVideoDkService.checkUserIsDk(userId,businessId);
    }
}
